package com.inventive.jmscache.server;

import javax.jms.*;
import javax.jms.IllegalStateException;

/**
 * @author Grant Little grant.little@inventivesoftware.com.au
 */
public class JmsCacheConnection implements Connection {

    protected ExceptionListener exceptionListener;
    protected String clientID;
    protected boolean closed = false;
    protected boolean started = false;
    protected JmsCacheServer jmsCacheServer;

    public JmsCacheConnection(JmsCacheServer jmsCacheServer) {
        this.jmsCacheServer = jmsCacheServer;
    }

    public Session createSession(boolean transacted, int acknowledgeMode) throws JMSException {
        checkClosed();
        return new JmsCacheSession(jmsCacheServer);
    }

    public String getClientID() throws JMSException {
        return clientID;
    }

    public void setClientID(String clientID) throws JMSException {
        checkClosed();
        this.clientID = clientID;
    }

    public ConnectionMetaData getMetaData() throws JMSException {
        return null;
    }

    public ExceptionListener getExceptionListener() throws JMSException {
        return exceptionListener;
    }

    public void setExceptionListener(ExceptionListener exceptionListener) throws JMSException {
        checkClosed();
        this.exceptionListener = exceptionListener;
    }

    public void start() throws JMSException {
        if (!started) {
            started = true;
        }
    }

    public void stop() throws JMSException {
        if (!started) {
            throw new IllegalStateException("Connection not started");
        }
        started = false;
    }

    public void close() throws JMSException {
        checkClosed();
        this.closed = true;
    }

    public ConnectionConsumer createConnectionConsumer(Destination destination, String s, ServerSessionPool serverSessionPool, int i) throws JMSException {
        checkClosed();
        return null;
    }

    public ConnectionConsumer createDurableConnectionConsumer(Topic topic, String s, String s1, ServerSessionPool serverSessionPool, int i) throws JMSException {
        checkClosed();
        return null;
    }

    protected void checkClosed() throws JMSException {
        if (closed) {
            throw new IllegalStateException("Connection is closed");
        }
    }

}
